package com.aries.ui.helper.navigation;


import com.aries.ui.util.StatusBarUtil;
import com.aries.ui.view.viewgroup.View;
import ohos.aafwk.ability.Ability;
import ohos.agp.components.ComponentContainer;
import ohos.agp.utils.Point;
import ohos.agp.window.service.*;
import ohos.app.Context;
import ohos.bundle.AbilityInfo;
import ohos.global.configuration.Configuration;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Optional;

/**
 * @Author: AriesHoo on 2018/7/19 9:32
 * @E-Mail: AriesHoo@126.com
 * Function: 导航栏控制工具类
 */
public class NavigationBarUtil {

    public static final int NAVIGATION_BAR_TYPE_DEFAULT = 0;
    public static final int NAVIGATION_BAR_TYPE_MI_UI = 1;
    public static final int NAVIGATION_BAR_TYPE_PHONE_O = 2;
    private volatile static float mAspectRatio = 1.97f;

    /**
     * 判断导航栏是否在底部
     * @param ability  context
     * @param window window
     * @return 在底部
     */
    public static boolean isNavigationAtBottom(Context ability, Window window) {
        if (window == null) {
            return false;
        }
        AbilityInfo.DisplayOrientation displayOrientation = ability.getAbilityInfo().getOrientation();
//        boolean mInPortrait = (Resources.getSystem().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT);
        boolean mInPortrait = (displayOrientation.equals(AbilityInfo.DisplayOrientation.PORTRAIT));
        float mSmallestWidthDp = getSmallestWidthDp(ability);
        return (mSmallestWidthDp >= 600 || mInPortrait);
    }

    /**
     * 判断当前手机是否为全面屏--通过纵横比
     *
     * @param context context
     * @return  纵横比
     */
    public static boolean isFullScreenDevice(Context context) {
        if (context == null) {
            return false;
        }
        return getAspectRatio(context) >= mAspectRatio;
    }

    /**
     * 获取手机纵横比
     *
     * @param context  context
     * @return  纵横比
     */
    public static float getAspectRatio(Context context) {
        if (context == null) {
            return 0f;
        }
        Display display = DisplayManager.getInstance().getDefaultDisplay(context).get();
        Point point = new Point();
        display.getRealSize(point);
        float width, height;
        if (point.getPointX() < point.getPointY()) {
            width = point.getPointX();
            height = point.getPointY();
        } else {
            width = point.getPointY();
            height = point.getPointX();
        }
        return height / width;
    }

    /**
     * 判断手机是否开启全面屏手势--根据判断系统是否开启虚拟导航栏(如华为可手动开关该方法不是完全正确的)
     *
     * @param context  context
     * @return  拟导航栏
     */
    public static boolean isOpenFullScreenGestures(Context context) {
        if (context == null || !isFullScreenDevice(context)) {
            return false;
        }
        return StatusBarUtil.isNavigationBarOpened(context);
    }

    public static boolean hasNavBar(ComponentContainer root) {
        if (root == null) {
            return false;
        }
        if (isOpenFullScreenGestures(root.getContext())) {
            return false;
        }
        if (isHideNavigationBar(root)) {
            return false;
        }
        //其他手机根据屏幕真实高度与显示高度是否相同来判断
        Display display = DisplayManager.getInstance().getDefaultDisplay(root.getContext()).get();
        DisplayAttributes attr = display.getAttributes();
        DisplayAttributes realAttr = display.getRealAttributes();
        return (realAttr.width - attr.width) > 0 || (realAttr.height - attr.height) > 0;
    }

    public static int getNavigationBarHeight(Context context){
        return StatusBarUtil.getNavigationBarHeight(context);
    }

    public static boolean isHideNavigationBar(ComponentContainer root) {
        if (root == null) {
            return false;
        }
        return (root.getVisibility() & WindowManager.LayoutConfig.MARK_TRANSLUCENT_NAVIGATION)
                == WindowManager.LayoutConfig.MARK_TRANSLUCENT_NAVIGATION;
    }

    private static float getSmallestWidthDp(Context context) {
//        Display display = DisplayManager.getInstance().getDefaultDisplay(context).get();
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
//            window.getWindowManager().getDefaultDisplay().getRealMetrics(metrics);
//        } else {
//            // TODO this is not correct, but we don't really care pre-kitkat
//            window.getWindowManager().getDefaultDisplay().getMetrics(metrics);
//        }

        Optional<Display> display = DisplayManager.getInstance().getDefaultDisplay(context);
        Point pt = new Point();
        if(display.isPresent()){
            display.get().getSize(pt);
            float widthDp = display.get().getAttributes().width / display.get().getAttributes().densityPixels;
            float heightDp = display.get().getAttributes().height / display.get().getAttributes().densityPixels;
            return Math.min(widthDp, heightDp);
        }
        return 0;
    }

    /**
     * 设置导航栏栏浅色模式--黑色字体图标，
     *
     * @param window  window
     * @return  浅色模式-
     */
    public static int setNavigationBarLightMode(Window window) {
        if (window == null) {
            return -1;
        }
        int result = NAVIGATION_BAR_TYPE_DEFAULT;

        return result;
    }
}
